#import "DiceGame.h"

@interface DiceGame ()

@property BOOL isGameOver;
@property NSMutableArray* images;
@property NSInteger currentImageToChange;
@property NSInteger redSum;
@property NSInteger blackSum;

@end

@implementation DiceGame

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setName:@"DiceGame"];
        [self setType:reaction];
        self.ruleOfTheGame = @"Hit when red = black!";
        self.images=[[NSMutableArray alloc]init];

        
    }
    return self;
}


-(void)draw{
    [self removeAllComponents];
    
    
    for (int coloum=0; coloum<3; coloum++) {
        for (int row=0; row<2; row++) {
            
            NSString* imageString;
            int imageNumber=arc4random_uniform(12) + 1;
            if (imageNumber < 7) {
                imageString=[NSString stringWithFormat:@"redDice%d.png",imageNumber];
                self.redSum=self.redSum+imageNumber;
            }
            else{
                imageString=[NSString stringWithFormat:@"blackDice%d.png",imageNumber-6];
                self.blackSum=self.blackSum+imageNumber-6;
            }
            
            
            self.images[self.currentImageToChange]=imageString;
            
            NSInteger imageSize=kScreenWidth/3-kWidthMargin;
            
            ImageGameComponent* image=[[ImageGameComponent alloc]initWithName:imageString andFrame:CGRectMake(89+(coloum*imageSize), 100+(row*imageSize), imageSize, imageSize)andTrans:0];
            [self addComponents:image];
            self.currentImageToChange++;
            
        }
    }
    
    self.currentImageToChange=0;
    if ([self isRight]) {
        [self draw];
    }
}


-(BOOL)isRight{
    return (self.blackSum==self.redSum);
}



-(void)play{
    [self draw];
    
    [NSTimer scheduledTimerWithTimeInterval: 1.5                                                 target: self selector:@selector(newDice:)userInfo: nil repeats:YES];
    
}

-(void)newDice:(NSTimer*)timer{
    if (self.isGameOver==YES) {
        [timer invalidate];
    }
    else{
        [self changeOneImage];
        
        [[NSNotificationCenter defaultCenter]postNotificationName:@"SomeGames" object:self userInfo:nil];
        
    }
    
}

-(void)gameOver{
    self.isGameOver=YES;
}


-(void)changeOneImage{

    NSString* imageString;
    int imageNumber=arc4random_uniform(6) + 1;
    if (self.redSum < self.blackSum) {
        imageString=[NSString stringWithFormat:@"redDice%d.png",imageNumber];
        self.redSum=self.redSum+imageNumber;
    }
    else{
        imageString=[NSString stringWithFormat:@"blackDice%d.png",imageNumber];
        self.blackSum=self.blackSum+imageNumber;
    }
    
    NSString* subString=self.images[self.currentImageToChange];
    if ([subString rangeOfString:@"red"].length > 0) {
        subString=[subString substringFromIndex:7];
        subString=[subString substringToIndex:1];

        NSInteger number=[subString integerValue];
        self.redSum=self.redSum-number;
    } else {
        subString=[subString substringFromIndex:9];
        subString=[subString substringToIndex:1];
        NSInteger number=[subString integerValue];
        self.blackSum=self.blackSum-number;
    }
    
    self.images[self.currentImageToChange]=imageString;

    ImageGameComponent* newComp=self.components[self.currentImageToChange];
    newComp.name=imageString;
    self.currentImageToChange++;
    
    if (self.currentImageToChange == 6) {
        self.currentImageToChange=0;
    }
    
    
}
@end
